package com.mdb.open.epay.service.epay;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mdb.bc.database.mybatisplus.SuperService;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.open.epay.common.MdbDataSource;
import com.mdb.open.epay.domain.bo.QueryOrderBo;
import com.mdb.open.epay.domain.po.epay.QpRefundOrder;
import com.mdb.open.epay.enums.status.QpRefundOrderStatusEnum;
import com.mdb.open.epay.mapper.epay.QpRefundOrderMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类描述：
 *  快捷退款订单 服务类
 *
 * @author liuyi
 * @date 2019-11-14
 */
@Service
@DS(MdbDataSource.EPAY)
public class QpRefundOrderService extends SuperService<QpRefundOrderMapper, QpRefundOrder> {

    public List<QpRefundOrder> queryRefundCancel(QueryOrderBo queryBo) {
        LambdaQueryWrapper<QpRefundOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(ObjectUtil.isNotEmpty(queryBo.getBeginDate()), QpRefundOrder::getCreateTime, queryBo.getBeginDate());
        wrapper.le(ObjectUtil.isNotEmpty(queryBo.getEndDate()), QpRefundOrder::getCreateTime, queryBo.getEndDate());
        if(ObjectUtil.isNotEmpty(queryBo.getOrderNoList())) {
            String[] orderNo = queryBo.getOrderNoList().split(",");
            wrapper.in(QpRefundOrder::getCustOrderNo, orderNo);
        }
        wrapper.eq(QpRefundOrder::getType, queryBo.getBizType());
        wrapper.eq(QpRefundOrder::getCustomerCode, queryBo.getCustomerCode());

        wrapper.in(QpRefundOrder::getStatus, QpRefundOrderStatusEnum.SUCCESS.getValue(), QpRefundOrderStatusEnum.FAIL.getValue());

        return this.baseMapper.selectList(wrapper);
    }

    /**
     * 根據狀態查詢退款訂單
     * @param status
     * @return
     */
    public List<QpRefundOrder> getByStatus(String status) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<QpRefundOrder>().eq(QpRefundOrder::getStatus, status));
    }


    public QpRefundOrder getByInnerOrderNo(String innerOrderNo) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<QpRefundOrder>().eq(QpRefundOrder::getInnerOrderNo, innerOrderNo));
    }

    public QpRefundOrder getByInnerOriOrderNo(String oriInnerOrderNo) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<QpRefundOrder>().eq(QpRefundOrder::getInnerOriOrderNo, oriInnerOrderNo));
    }

    public boolean existed(String customerCode, String orderNo) {
        return 0 < this.count(new LambdaUpdateWrapper<QpRefundOrder>().eq(QpRefundOrder::getCustOrderNo, orderNo).eq(QpRefundOrder::getCustomerCode, customerCode));
    }
}
